#include <REG51F.H>
#include "Timer\timerLedCube.h"

/*********** Control Leb cube **********/
/* 	Use P0 and P1 for  Column- Row ,P2 for Floor   (Timer 1)
	P3 for Scan key	   (Timer 0)
	 */
   sbit Led4 =P3^4;
   sbit Led5 =P3^5;
   sbit Led6 =P3^6;
   sbit Led7 =P3^7;
unsigned char pwd[4];
unsigned char key;
unsigned char preKey;
unsigned char checkpass;
unsigned char dataKey[4]={0xFE,0xFD,0xFB,0xF7}; 		  //set cac hang xuong 0
unsigned char scankeypad() ;
void init();
void main(void) { 
	init();
	
	while(1)  { 
		key =  scankeypad();
	}
	
} 
void init(){ //Timer, P0, P1, P2, P3
	P2=0x00;
	P1=0x00;
	P0=0x00;
	Init_Timer0();
	EA = 1;
}

unsigned char scankeypad() { 
		/*********************
		0	|	1		2		3
		1	|	4		5		6
		2	|	7		8		9
		3	|	*(10)	0		#(12)
			  --------------------
				Led6	led5	Led4
		**********************/
	unsigned char value=15;
	unsigned char x,y;   
	for(x=0; x<4;x++){ 
		P3=dataKey[x];
		for(y=0;y<4;y++){ 
			if(Led4==0) 
						value=3*x+3;	 
			if(Led5==0)
				if(x!=3)	value=3*x+2;
				else	value=0;					
			if(Led6==0)
						value=3*x+1;	
		} 		
	} 
	return(value);  
}


/* 
void delay(long i){
while (i--);
}

void display(){
	P1&=0x00;//clear
	P1=input[3]<<4;
	P1|=input[2];
	
	P0&=0x00;//clear
	P0=input[1]<<4;
	P0|=input[0];		
}


void waitOpen_Close(){
	timeOut=0;
	Start_Timer0();
	while(1){	
		if( timeOut == 1000||(P2&0x80)!=0)
			break;
	}
	resetLed7();
	Stop_Timer0();
}

void setPass(){
	reset();
	while(submit<2)  { 
		key=scankeypad();
		if(preKey!=key){
				preKey=key;//Chong rung
				display(); 
			if(key!=15){
				if(key==10){// Phim *
					//exit;
					reset();
					submit=2;
						
				}else if(key==12){// Phim #
					
					if(submit==0){				
							input[4]=input[0];
							input[5]=input[1];
							input[6]=input[2];
							input[7]=input[3];
							submit++;
							P2|=0x03;
							resetLed7();
					}else{
						checkpass=1;
						for(key=0;key<4;key++){
							if(input[key]!=input[key+4]){
								checkpass=0;
								break;
							}		
						}
						if(checkpass==1){//Confirm password
							P2&=0xFE;
							P2|=0x01;//Set P1.0
							pwd[0]=input[4];
							pwd[1]=input[5];
							pwd[2]=input[6];
							pwd[3]=input[7];
							delay(10000);
							reset();
							submit=2;
						}else{
							P2|=0x02;
							submit=0;
							delay(10000);
							reset();
						}	
					}

				}else{
					input[3]=input[2];
					input[2]=input[1];
					input[1]=input[0];
					input[0]=key;
					time++;
					if(time>4){
						time=0;
						reset();
					}
				}
			}
		}
		
	} 
}
  */
